package org.wk.upms.controller;

import com.google.common.base.Strings;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wk.core.common.view.PageVo;
import org.wk.core.common.view.ResponseVo;
import org.wk.core.web.exception.GeneralException;
import org.wk.logger.common.annotation.SysLog;
import org.wk.upms.service.ManagerRoleService;
import org.wk.upms.service.ManagerService;
import org.wk.upms.shiro.ShiroUtils;
import org.wk.upms.view.ManagerVo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
	@Autowired
	private ManagerService sysUserService;
	@Autowired
	private ManagerRoleService sysUserRoleService;

	/**
	 * 所有用户列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:user:list")
	public ResponseVo list(@RequestParam Map<String, Object> params){
		PageVo page = sysUserService.queryPage(params);

		return ResponseVo.success().addAttribute("page", page);
	}

	/**
	 * 获取登录的用户信息
	 */
	@RequestMapping("/info")
	public ResponseVo info(){
		return ResponseVo.success().addAttribute("user", getUser());
	}

	/**
	 * 修改登录用户密码
	 */
	@SysLog("修改密码")
	@RequestMapping("/password")
	public ResponseVo password(String password, String newPassword){
	    if (Strings.isNullOrEmpty(password) || Strings.isNullOrEmpty(newPassword)) {
	        throw new GeneralException("密码不能为空");
        }
		//原密码
		password = ShiroUtils.sha256(password, getUser().getSalt());
		//更新密码
		ResponseVo result = sysUserService.updatePassword(getUserId(), password, newPassword);
		return result;
	}

	/**
	 * 用户信息
	 */
	@RequestMapping("/info/{userId}")
	@RequiresPermissions("sys:user:info")
	public ResponseVo info(@PathVariable("userId") String userId){
		ManagerVo user = sysUserService.selectById(userId);

		//获取用户所属的角色列表
		List<String> roleIdList = sysUserRoleService.queryRoleIdList(userId);
		user.setRoleIds(roleIdList);

		return ResponseVo.success().addAttribute("user", user);
	}

	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@RequestMapping("/save")
	@RequiresPermissions("sys:user:save")
	public ResponseVo save(@RequestBody ManagerVo user){
//		ValidatorUtils.validateEntity(user, AddGroup.class);

		sysUserService.save(user);

		return ResponseVo.success();
	}

	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@RequestMapping("/update")
	@RequiresPermissions("sys:user:update")
	public ResponseVo update(@RequestBody ManagerVo user){
//		ValidatorUtils.validateEntity(user, UpdateGroup.class);

		sysUserService.update(user);

		return ResponseVo.success();
	}

	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:user:delete")
	public ResponseVo delete(@RequestBody String[] userIds){
		if(ArrayUtils.contains(userIds, "admin")){
			return ResponseVo.error("系统管理员不能删除");
		}

		if(ArrayUtils.contains(userIds, getUserId())){
			return ResponseVo.error("当前用户不能删除");
		}

		sysUserService.deleteBatchIds(Arrays.asList(userIds));

		return ResponseVo.success();
	}

	public static void main(String[] args) {
		String salt = RandomStringUtils.randomAlphanumeric(20);
		String ss = ShiroUtils.sha256("111111", salt);
		System.out.println(salt);
		System.out.println(ss);
	}
}
